package com.example.sell_qq.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.sell_qq.R;
import com.example.sell_qq.constant.ApiConstant;
import com.example.sell_qq.util.HttpUtils;
import com.example.sell_qq.vo.ResponseVO;

import java.io.IOException;
import java.util.Map;

import static com.example.sell_qq.util.AppUtils.isQQClientAvailable;

public class HomeFragment extends Fragment {

    private TextView announcement;


    private Button group1;
    private Button group2;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View fragment = inflater.inflate(R.layout.home_layout, container, false);
        announcement = fragment.findViewById(R.id.announcement);
        try {
            String response = HttpUtils.httpGet(ApiConstant.EXPORT_API + "announcement/");
            ResponseVO responseVO = HttpUtils.resolveResponse(response);
            String content = ((Map<String, String>) responseVO.getData()).get("content");
            announcement.setText(content);
        } catch (IOException e) {
            e.printStackTrace();
        }

        group1 = fragment.findViewById(R.id.group1);
        group2 = fragment.findViewById(R.id.group2);
        final String[] group = new String[2];
        try {
            String response = HttpUtils.httpGet(ApiConstant.EXPORT_API + "group/");
            ResponseVO responseVO = HttpUtils.resolveResponse(response);
            group[0] = ((Map<String, String>) responseVO.getData()).get("first");
            group[1] = ((Map<String, String>) responseVO.getData()).get("second");
        } catch (IOException e) {
            e.printStackTrace();
        }

        group1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isQQClientAvailable(getContext())) {
                    joinQQGroup(group[0]);
                } else {
                    Toast.makeText(getContext(), "请安装QQ客户端", Toast.LENGTH_SHORT).show();
                }
            }
        });
        group2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isQQClientAvailable(getContext())) {
                    joinQQGroup(group[1]);
                } else {
                    Toast.makeText(getContext(), "请安装QQ客户端", Toast.LENGTH_SHORT).show();
                }
            }
        });

        return fragment;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    /****************
     *
     * @param key 由官网生成的key
     * @return 返回true表示呼起手Q成功，返回false表示呼起失败
     ******************/
    public boolean joinQQGroup(String key) {
        Intent intent = new Intent();
        intent.setData(Uri.parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3D" + key));
        // 此Flag可根据具体产品需要自定义，如设置，则在加群界面按返回，返回手Q主界面，不设置，按返回会返回到呼起产品界面    //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        try {
            startActivity(intent);
            return true;
        } catch (Exception e) {
            // 未安装手Q或安装的版本不支持
            return false;
        }
    }


}
